package com.stary.beetlsql.generator.config.querys;

import java.util.Map;

import com.stary.beetlsql.generator.config.IDbQuery;
import com.stary.beetlsql.generator.config.rules.DBType;
import com.stary.beetlsql.generator.util.StringUtils;

/**
 * <p>sql_server 查询</p>
 * @author stary {@link stary1993@qq.com}
 * @since 2019-8-6
 */
public class SqlServerQuery implements IDbQuery {

	@Override
	public DBType dbType() {
		return DBType.SQL_SERVER;
	}

	@Override
	public String tablesSql(String tableName) {
		String sql = " select cast(so.name as varchar(500)) as TABLE_NAME, " +
            "cast(sep.value as varchar(500)) as COMMENTS from sysobjects so " +
            "left JOIN sys.extended_properties sep on sep.major_id=so.id and sep.minor_id=0 " +
            "where (xtype='U' or xtype='v') ";
		if (!StringUtils.isEmpty(tableName)) {
			sql += "  and so.name = '" + tableName + "' ";
		}
		return sql;
	}

	@Override
	public String tableFieldsSql(String tableName, String colName) {
		String sql = " SELECT  cast(a.NAME AS VARCHAR(500)) AS TABLE_NAME,cast(b.NAME AS VARCHAR(500)) AS COLUMN_NAME, "
            + "cast(c.VALUE AS VARCHAR(500)) AS COMMENTS,cast(sys.types.NAME AS VARCHAR (500)) AS DATA_TYPE,"
            + "(" + " SELECT CASE count(1) WHEN 1 then 'PRI' ELSE '' END"
            + " FROM syscolumns,sysobjects,sysindexes,sysindexkeys,systypes "
            + " WHERE syscolumns.xusertype = systypes.xusertype AND syscolumns.id = object_id (A.NAME) AND sysobjects.xtype = 'PK'"
            + " AND sysobjects.parent_obj = syscolumns.id " + " AND sysindexes.id = syscolumns.id "
            + " AND sysobjects.NAME = sysindexes.NAME AND sysindexkeys.id = syscolumns.id "
            + " AND sysindexkeys.indid = sysindexes.indid "
            + " AND syscolumns.colid = sysindexkeys.colid AND syscolumns.NAME = B.NAME) as 'KEY',"
            + "  b.is_identity isIdentity "
            + " FROM ( select name,object_id from sys.tables UNION all select name,object_id from sys.views ) a "
            + " INNER JOIN sys.COLUMNS b ON b.object_id = a.object_id "
            + " LEFT JOIN sys.types ON b.user_type_id = sys.types.user_type_id   "
            + " LEFT JOIN sys.extended_properties c ON c.major_id = b.object_id AND c.minor_id = b.column_id "
            + " WHERE a.NAME = '" + tableName + "' and sys.types.NAME !='sysname' ";
		if (!StringUtils.isEmpty(colName)) {
			sql += " and b.NAME = '" + colName + "' ";
		}
		return sql;
	}

	@Override
	public String tableName() {

		return "TABLE_NAME";
	}

	@Override
	public String tableComment() {

		return "COMMENTS";
	}

	@Override
	public String fieldName() {

		return "COLUMN_NAME";
	}

	@Override
	public String fieldType() {

		return "DATA_TYPE";
	}

	@Override
	public String fieldComment() {

		return "COMMENTS";
	}

	@Override
	public String fieldKey() {

		return "KEY";
	}

	@Override
	public boolean isKeyIdentity(Map<?, ?> results) {		
		 return results.get("isIdentity").equals(1);
	}


}
